package cn.tedu.tmall.front.account.service.impl;

import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.front.account.dao.persist.repository.IUserRepository;
import cn.tedu.tmall.front.account.pojo.entity.User;
import cn.tedu.tmall.front.account.pojo.vo.UserStandardVO;
import cn.tedu.tmall.front.account.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserService implements IUserService {
    @Autowired
    private IUserRepository userRepository;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public void updatePassword(CurrentPrincipal currentPrincipal, String oldPassword, String newPassword) {
        log.debug("开始处理修改密码");
        Long userId = currentPrincipal.getId();
        UserStandardVO queryResult = userRepository.getStandardById(userId);
        String dbPassword = queryResult.getPassword();
        boolean matches = passwordEncoder.matches(oldPassword, dbPassword);
        // 判断用户的原密码是否正确
        if (!matches){
            String message="修改密码失败,原密码错误";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        // 如果用户的原密码正确,则将用户新密码设置为密文
        String encodePassword = passwordEncoder.encode(newPassword);
        User user=new User();
        user.setId(userId);
        user.setPassword(encodePassword);
        int i = userRepository.updateById(user);
        if (i!=1){
            String message="修改密码失败,服务器繁忙";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public void updateAvatar(CurrentPrincipal currentPrincipal, String avatar) {
        log.debug("开始修改头像");
        Long userId = currentPrincipal.getId();
        User user=new User();
        user.setId(userId);
        user.setAvatar(avatar);
        int i = userRepository.updateById(user);
        if (i!=1){
            String message="修改头像失败,服务器繁忙";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public void updatePhone(CurrentPrincipal currentPrincipal, String phone) {
        log.debug("开始处理修改手机号");
        Long userId = currentPrincipal.getId();
        // 判断手机号是否被占用
        int count = userRepository.countByPhoneAndNotId(phone, userId);
        if (count>0){
            String message="修改手机号失败,手机号被占用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        User user=new User();
        user.setId(userId);
        user.setPhone(phone);
        int i = userRepository.updateById(user);
        if(i!=1){
            String message="修改手机号失败,服务器繁忙";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public void updateEmail(CurrentPrincipal currentPrincipal, String email) {
        log.debug("开始处理修改邮箱");
        Long userId = currentPrincipal.getId();
        // 判断邮箱是否被占用
        int count = userRepository.countByEmailAndNotId(email, userId);
        if (count>0){
            String message="修改邮箱失败,邮箱被占用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        User user=new User();
        user.setId(userId);
        user.setEmail(email);
        int i = userRepository.updateById(user);
        if (i!=1){
            String message="修改邮箱失败,服务器繁忙";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }


}
